package com.example.administrator.capital.model;
import com.example.administrator.capital.R;
import com.example.administrator.capital.model.base.BaseModel;
import com.example.administrator.capital.model.base.IActionRequest;
import com.example.administrator.capital.model.bean.ErrorBean;
import com.example.administrator.capital.url.Url;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.LogUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2017/12/19 0019.
 *   model
 *
 */
public class ItemsModel extends BaseModel {
    /**
     * @param iActionRequest
     * 项目列表接口
     */
    public void project(final IActionRequest<String> iActionRequest,int page){
        OkGo.<String>post(Url.PROJECT).tag(this).params("pageNo",page).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("项目列表"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }

        });
    }
    /**
     * @param iActionRequest
     * 项目详情
     * */
    public void details(final IActionRequest<String> iActionRequest,String proid){
        OkGo.<String>post(Url.DETAILS).tag(this).headers("TOKEN", PreferenceManager.instance().getToken()).params("proid",proid).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("项目详情"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }
    /**
     * @param iActionRequest
     * 项目详情
     * */
    public void chujie(final IActionRequest<String> iActionRequest,String proid,String tzamount,String pwd){
        OkGo.<String>post(Url.ADDORDER).tag(this)
                .headers("TOKEN", PreferenceManager.instance().getToken())
                .params("proid",proid)
                .params("tzamount",tzamount)
                .params("pwd",pwd)
                .execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }
    /**
     * @param iActionRequest
     * @param id
     * 登录详情界面
     */
    public void getCardDetail(final IActionRequest<String> iActionRequest,String id,String token){
        OkGo.<String>post(Url.CARD_DETAIL+id).tag(this).params("token",token).
        params("did", PreferenceManager.instance().getOpenId()).
                params("uid",PreferenceManager.instance().getUseId()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("登录后信用卡详情"+response.body());
                if (response.body().contains("\"code\":200")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }

    public void onlinepay(final IActionRequest<String> iActionRequest,String uid,String tranAmt){
        OkGo.<String>post(Url.ONLINEPAY).tag(this)
                .params("uid",uid)
                .params("tranAmt",tranAmt)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("充值"+response.body());

//                        if (response.body().contains("\"code\":1")) {
                            iActionRequest.success(response);
//                        } else {
//                            ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
//                            iActionRequest.failMsg(bean.getMsg());
//                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        iActionRequest.code(R.string.error_msg);
                    }
                });
    }
    /**
     * @param iActionRequest
     * 收件
     */
    public void getCardFilter(final IActionRequest<String> iActionRequest,String id){
        OkGo.<String>post(Url.CARDS_FILTER).tag(this).params("OID",id)
                .params("did", PreferenceManager.instance().getOpenId()).
                params("uid",PreferenceManager.instance().getUseId())
                .execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("收件"+response.body());
                if (response.body().contains("\"resultflag\":\"s\"")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }
    /**
     * @param iActionRequest


     *信用卡列表
     *  */
    public void getCardList(final IActionRequest<String> iActionRequest,String status,String OID){
        OkGo.<String>post(Url.CARDS_LIST).tag(this).params("status",status).params("OID",OID).params("did", PreferenceManager.instance().getOpenId()).params("uid",PreferenceManager.instance().getUseId()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("提交订单"+response.body());
                if (response.body().contains("\"resultflag\":\"s\"")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }
}
